﻿@{
	PageData["Title"] = "Edit Feature";
    
    WebSecurity.RequireAuthenticatedUser();
    if (!Roles.IsUserInRole("admin") || UrlData[0].IsEmpty()) {
    	Response.Redirect("~");
    }    	
    
    var name = "";
	var description = "";	
	var errorMessage = "";
    
    if(IsPost) {
        if (Request["action"] == "Update") {
            name = Request["name"];
            description = Request["description"];
            
            if (name.IsEmpty()) {
                errorMessage = "Name cannot be empty.";
            }
            
            //If no errors in validation, continue with registration
            if (errorMessage.IsEmpty()) {
                try {
                    // Update a feature using Freemium Helper
                    Freemium.UpdateFeature(name, description);
                    Response.Redirect("~/Freemium/Admin");
                } catch (Exception ex) {
                    errorMessage = "Sorry, we ran into a problem processing your request.";
                }
            } 
        } else if (Request["action"] == "Delete") {
            name = Request["name"];
            Freemium.RemoveFeature(name);
            Response.Redirect("~/Freemium/Admin");
        }
    } else {        
        name = UrlData[0];
        try {
            var feature = Freemium.GetFeature(name);
            name = feature.name;
            description = feature.description;
        } catch(Exception ex) {
            errorMessage = "Sorry, we ran into a problem processing your request.";
        }
    }
}

@if (!errorMessage.IsEmpty()) {
<div class="error">
    @errorMessage
</div>
}

<h1>Edit Feature</h1>
Use the form below to modify the Feature.<br/>
<script type="text/javascript">
	function deleteFeature(name) {
        if (confirm("Are you sure you want to delete the Feature '" + name +  "' ?"))
        {
            document.forms['deleteFeatureForm'].submit();
        }
	}
</script>
<div>
    <form id="validateForm" method="post" action="">
	    <fieldset>
		    <legend>Edit Feature</legend>
			<div class="freemiumLabel">
				<label class="" for="name" >Name</label>
			</div>
			<div class="freemiumInput">
                <input name="name" type="hidden" value="@name" />
				<input name="name" type="text" value="@name" disabled />
			</div>
			<div class="freemiumLabel">
				<label for="description">Description</label>
			</div>
			<div class="freemiumInput">
				<input id="description" name="description" value="@description" type="text" maxlength="1000" />
			</div>
                       
            <div class="freemiumSubmitGroup">
                <input type="submit" name="action" value="Update" />
                <input type="button" onclick="javascript:deleteFeature('@name')" value="Delete"/>
                <input type="button" name="action" value="Cancel" onclick="javascript:window.location='@Href("~/Freemium/Admin")'" />
            </div>
        </fieldset>                
    </form>
</div>
 <form id="deleteFeatureForm" method="post" action="">
    <input name="name" type="hidden" value="@name" />
    <input name="action" type="hidden" value="Delete"/>
</form>